package com.guiyang.practice.mess1.controller.message;

import com.guiyang.practice.mess1.entity.MessageType;
import com.guiyang.practice.mess1.service.MessageTypeService;
import com.guiyang.practice.mess1.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/message/type")
public class MessageTypeController {

    @Autowired
    private MessageTypeService messageTypeService;

    /**
     * 获取所有消息类型
     */
    @GetMapping("/list")
    public ResultVO<List<MessageType>> getAllTypes() {
        try {
            List<MessageType> types = messageTypeService.getAllTypes();
            return ResultVO.success("获取消息类型列表成功", types);
        } catch (Exception e) {
            return ResultVO.error(500, "获取消息类型列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取启用的消息类型
     */
    @GetMapping("/enabled")
    public ResultVO<List<MessageType>> getEnabledTypes() {
        try {
            List<MessageType> types = messageTypeService.getEnabledTypes();
            return ResultVO.success("获取启用消息类型成功", types);
        } catch (Exception e) {
            return ResultVO.error(500, "获取启用消息类型失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取消息类型
     */
    @GetMapping("/{id}")
    public ResultVO<MessageType> getTypeById(@PathVariable Long id) {
        try {
            MessageType type = messageTypeService.getTypeById(id);
            if (type != null) {
                return ResultVO.success("获取消息类型成功", type);
            }
            return ResultVO.error(404, "消息类型不存在");
        } catch (Exception e) {
            return ResultVO.error(500, "获取消息类型失败: " + e.getMessage());
        }
    }

    /**
     * 新增消息类型
     */
    @PostMapping
    public ResultVO<?> addType(@RequestBody MessageType messageType) {
        try {
            boolean result = messageTypeService.addType(messageType);
            if (result) {
                return ResultVO.success("添加成功");
            }
            return ResultVO.error(400, "添加失败");
        } catch (RuntimeException e) {
            return ResultVO.error(400, e.getMessage());
        } catch (Exception e) {
            return ResultVO.error(500, "添加消息类型失败: " + e.getMessage());
        }
    }

    /**
     * 更新消息类型
     */
    @PutMapping
    public ResultVO<?> updateType(@RequestBody MessageType messageType) {
        try {
            boolean result = messageTypeService.updateType(messageType);
            if (result) {
                return ResultVO.success("更新成功");
            }
            return ResultVO.error(400, "更新失败");
        } catch (RuntimeException e) {
            return ResultVO.error(400, e.getMessage());
        } catch (Exception e) {
            return ResultVO.error(500, "更新消息类型失败: " + e.getMessage());
        }
    }

    /**
     * 禁用消息类型
     */
    @DeleteMapping("/{id}")
    public ResultVO<?> disableType(@PathVariable Long id) {
        try {
            boolean result = messageTypeService.disableType(id);
            if (result) {
                return ResultVO.success("禁用成功");
            }
            return ResultVO.error(400, "禁用失败");
        } catch (Exception e) {
            return ResultVO.error(500, "禁用消息类型失败: " + e.getMessage());
        }
    }

    /**
     * 启用消息类型
     */
    @PostMapping("/{id}/enable")
    public ResultVO<?> enableType(@PathVariable Long id) {
        try {
            boolean result = messageTypeService.enableType(id);
            if (result) {
                return ResultVO.success("启用成功");
            }
            return ResultVO.error(400, "启用失败");
        } catch (Exception e) {
            return ResultVO.error(500, "启用消息类型失败: " + e.getMessage());
        }
    }
}